package io.api.modules.login.controller;

import io.admin.common.utils.Result;
import io.admin.common.validator.ValidatorUtils;
import io.admin.common.validator.group.AddGroup;
import io.admin.common.validator.group.DefaultGroup;
import io.api.annotation.LoginUser;
import io.api.annotation.Pass;
import io.api.modules.login.dto.UserEquipmentDTO;
import io.api.modules.login.entity.UserEntity;
import io.api.modules.login.entity.UserEquipmentEntity;
import io.api.modules.login.service.UserEquipmentService;
import io.api.modules.login.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;


/**
 * 用户设备授权
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-08
 */
@RestController
@RequestMapping("/api/userequipment")
@AllArgsConstructor
@Api(tags="用户设备管理")
public class ApiUserEquipmentController {

    private final UserService userService;

    private final UserEquipmentService userEquipmentService;

    @Pass
    @PostMapping
    @ApiOperation("新增用户设备授权")
    public Result save(
            @RequestParam(required = false) String bumber,
            @RequestParam(required = false) String mobile
    ){
        UserEntity user = userService.getByMobile(mobile);
        UserEquipmentDTO dto = new UserEquipmentDTO();
        dto.setUserid(user.getId());
        dto.setPassword(user.getPassword());
        dto.setUserrole(user.getUserrole());
        dto.setState(0);
        userEquipmentService.save(dto);
        return new Result();
    }

    @Pass
    @GetMapping
    @ApiOperation("查询用户设备授权")
    public Result get(
            @RequestParam(required = false) String bumber,
            @RequestParam(required = false) String mobile
    ){
        UserEntity user = userService.getByMobile(mobile);
        UserEquipmentEntity userEquipmentEntity = userEquipmentService.getUserEquipmentEntity(user.getId(), bumber);
        if (Objects.isNull(userEquipmentEntity)) {
            return new Result<String>().error("查不到对应设备，请重新绑定");
        } else if (userEquipmentEntity.getState() != 1) {
            return new Result<String>().error("用户设备未授权，请申请授权");
        } else {
            return new Result<String>().ok("用户设备授权已授权");
        }

    }

}